var form, table, tableInstance;

$(function () {
    initLayUI(function () {
        initButtons();
    })
});

function initLayUI(cb) {
    layui.use(['table', 'form'], function () {
        table = layui.table;
        form = layui.form;

        form.render();

        tableInstance = table.render({
            elem: '#configTable',
            limits: [10, 15, 20],
            url: ServerURL + "/config/list",
            headers: tokenUtil.getAuthHeader(),
            method: "POST",
            toolbar: "#toolbar",
            cols: [[
                {field: 'configId', title: '参数编号', align: 'center', minWidth: 50},
                {field: 'configName', title: '参数名称', align: 'center', minWidth: 100},
                {field: 'configKey', title: '参数Key', align: 'center', minWidth: 100},
                {field: 'configValue', title: '参数值', align: 'center', minWidth: 100},
                {field: 'remark', title: '备注', align: 'center', minWidth: 150},
                {field: 'createTime', title: '创建时间', align: 'center', minWidth: 100},
                {field: 'oper', title: '操作', minWidth: 250, align: 'center', toolbar: "#operBar"}
            ]],
            contentType: 'application/json',
            response: {
                statusName: 'code',
                statusCode: 200,
                msgName: 'msg',
                countName: 'count',
                dataName: 'data'
            },
            loading: true,
            page: true,
            error: function (res) {
                ajaxUtil.handleError(res);
            }
        });

        table.on('tool(configTable)', function (obj) {
            if (obj.event === 'delete') {
                deleteConfig(obj);
            } else if (obj.event === 'update') {
                updateConfig(obj);
            }
        });

        table.on('toolbar(configTable)', function (obj) {
            if (obj.event === 'add') {
                addConfig();
            }
        });

        cb();
    });
}

function initButtons() {
    $("#searchBtn").click(function () {
        reloadTable();
    });

    $("#resetBtn").click(function () {
        form.val('form', {
            "configName": "",
            "configKey": ""
        });

        reloadTable();
    });
}

function reloadTable() {
    let data = form.val('form');
    tableInstance.reload({
        where: data,
        page: {
            curr: 1
        }
    });
}

function deleteConfig(obj) {
    layerUtil.confirm("确定要删除该参数么?", function () {
        ajaxUtil.simplePost({
            "configId": obj.data.configId
        }, "/config/delete", function () {
            reloadTable();
        });
    });
}

function updateConfig(obj) {
    putTempData('updateConfigId', obj.data.configId);
    iframeIndex = layerUtil.iframe({
        id: 'updateConfigFrame',
        title: "修改",
        area: ['650px', '450px'],
        content: "view/config/configEdit.html"
    });
}

function addConfig() {
    putTempData('updateConfigId');
    iframeIndex = layerUtil.iframe({
        id: 'addConfigFrame',
        title: "新增",
        area: ['650px', '450px'],
        content: "view/config/configEdit.html"
    });
}